#include <string>
#include <iostream>
using std::cout;
using std::endl;
using std::string;

template <class T,int kCapacity = 10>
class Stack
{
public:
    Stack()
    : _top(-1)
    , _data(new T[kCapacity]())
    {
        cout << "Stack()" << endl;
    }

    ~Stack(){
        if(_data){
            delete [] _data;
            _data = nullptr;
        }
        cout << "~Stack()" << endl;
    }

    bool empty() const;
    bool full() const;
    void push(const T &);
    void pop();
    T top();
private:
    int _top;
    T * _data;
};

template <class T, int kCapacity>
bool Stack<T,kCapacity>::empty() const{
    return _top == -1;
}

template <class T, int kCapacity>
bool Stack<T,kCapacity>::full() const{
    return _top == kCapacity - 1;
}

template <class T, int kCapacity>
void Stack<T,kCapacity>::push(const T & t){
    if(!full()){
        _data[++_top] = t;
    }else{
        cout << "Stack is full!" << endl;
    }
}

template <class T, int kCapacity>
void Stack<T,kCapacity>::pop(){
    if(!empty()){
        --_top;
    }else{
        cout << "Stack is empty" << endl;
    }
}

template <class T, int kCapacity>
T Stack<T,kCapacity>::top(){
    return _data[_top];
}


void test0(){
    //创建了一个容量为10，存放int元素的栈
    Stack<int> stack;

    //创建了一个容量为20，存放long元素的栈
    Stack<long,20> stack2;

    Stack<string,8> stack3;
    cout << stack3.empty() << endl;
    stack3.push(string("hello"));
    cout << stack3.empty() << endl;

    for(int idx = 1; idx < 8; ++idx){
        stack3.push(string(3,'a' + idx));
    }

    cout << stack3.empty() << endl;
    cout << stack3.full() << endl;

    while(!stack3.empty()){
        cout << stack3.top() << endl;
        stack3.pop();
    }

}

int main(void){
    test0();
    return 0;
}
